package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.entity.Dormitory;
import io.geekidea.springbootplus.system.entity.ParcelCenter;
import io.geekidea.springbootplus.system.param.DormitoryPageParam;
import io.geekidea.springbootplus.system.service.DormitoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-10-15
 */
@Slf4j
@RestController
@RequestMapping("/dormitory")
@Module("system")
@Api(value = "API", tags = {""})
public class DormitoryControllerExtra extends BaseController {

    @Autowired
    private DormitoryService dormitoryService;

    /**
     * 分页列表
     */
    @PostMapping("/getBySchool")
    @OperationLog(name = "所有列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "所有列表", response = Dormitory.class)
    public ApiResult<List<Dormitory>> getAll(@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        int schoolID = currentAdminInfo.getSchool().getSchoolID();
        LambdaQueryWrapper<Dormitory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dormitory::getSchoolID, schoolID).eq(Dormitory::getDeleted, 0);
        List<Dormitory> list = dormitoryService.getAllList(wrapper);
        return ApiResult.ok(list);
    }

    /**
     * 添加
     */
    @PostMapping("/new")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> addDormitory(@RequestBody JSONObject jsonObject, @CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        Dormitory dormitory = new Dormitory();
        dormitory.setName(jsonObject.getString("Name"));
        dormitory.setCreateTime(new Date());
        dormitory.setDeleted(0);
        dormitory.setCreatePersonID(currentAdminInfo.getAdmin().getAdminID());
        dormitory.setSchoolID(currentAdminInfo.getSchool().getSchoolID());
        try {
            boolean flag = dormitoryService.saveOrUpdate(dormitory);
        }catch (Exception e){
            dormitoryService.update(dormitory, new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getName, jsonObject.getString("Name")));
        }
        return ApiResult.result(true);
    }
}
